/**
 * Description
 *
 * Creation Date 31/10/2017
 * @author Candy
 */

'use strict';

import mongoose from 'mongoose';
import path from 'path';
import config from 'config-lite';
const configInfo = config(path.join(__dirname, 'config'));

mongoose.connect(configInfo.mongodb, {useMongoClient:true});
mongoose.Promise = global.Promise;

const db = mongoose.connection;

db.once('open', () => {
	console.log('连接数据库成功')
});

db.on('error', error => {
	console.error('Error in MongoDb connection: ' + error);
	mongoose.disconnect();
});

db.on('close', () => {
	console.log('数据库断开，重新连接数据库');
	mongoose.connect(configInfo.mongodb, {server:{auto_reconnect:true}});
});

export default db;


